package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.CurrentUserConstants;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.entity.AdType;
import com.yuwubao.uav.tool.entity.ThemeAd;
import com.yuwubao.uav.tool.entity.ThemeAdType;
import com.yuwubao.uav.tool.service.AdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/bk")
public class AdTypeApi {
    private Logger logger = LoggerFactory.getLogger(AdTypeApi.class);

    @Resource
    private AdService adService;

    @LoginRequired
    @GetMapping("getChildTypeById")
    JsonResult getChildTypeById(@RequestParam int id) {
        JsonResult result = new JsonResult();
        result.setResult(adService.getChildTypeById(id));
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @GetMapping("getParentAdTypes")
    JsonResult getParentAdTypes(@RequestParam(required = false, defaultValue = "1") int page,
                               @RequestParam(required = false, defaultValue = "10") int pageSize,
                               @RequestParam(required = false, defaultValue = "") String searchKey) {
        JsonResult result = new JsonResult();
        PageData pd = new PageData();
        pd.put("searchKey", searchKey);
        pd.put("index", (page - 1) * pageSize);
        pd.put("size", pageSize);
        result.setResult(adService.getParentAds(pd));
        result.setResultNum(adService.getParentAdsNum(pd));
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("editParentAdType")
    JsonResult editParentAdType(ThemeAdType type) {
        JsonResult result = new JsonResult();
        adService.editParentAdType(type);
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("delParentAdType")
    JsonResult delParentAdType(@RequestParam int id) throws Exception {
        JsonResult result = new JsonResult();
        adService.delParentAdType(id);
        result.setSuccessful();
        return result;
    }
    @LoginRequired
    @GetMapping("getAdTypes")
    JsonResult getAdTypes(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam(required = false, defaultValue = "") String searchKey
    ) {
        JsonResult result = new JsonResult();
        PageData pd = new PageData();
        pd.put("searchKey", searchKey);
        pd.put("index", (page - 1) * pageSize);
        pd.put("size", pageSize);
        result.setResult(adService.getAdTypes(pd));
        result.setResultNum(adService.getAdTypesNum(pd));
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("editType")
    JsonResult editType(AdType adType) {
        JsonResult result = new JsonResult();
        adService.editType(adType);
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("delType")
    JsonResult delType(@RequestParam int id) {
        JsonResult result = new JsonResult();
        adService.delType(id);
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("editState")
    JsonResult editState(@RequestParam int id) {
        JsonResult result = new JsonResult();
        adService.editState(id);
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("editThemeAd")
    JsonResult editThemeAd(ThemeAd ad) {
        JsonResult result = new JsonResult();
        adService.editThemeAd(ad);
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @GetMapping("getThemeAds")
    JsonResult getThemeAds(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam(required = false, defaultValue = "") String searchKey
    ) {
        JsonResult result = new JsonResult();
        PageData pd = new PageData();
        pd.put("searchKey", searchKey);
        pd.put("index", (page - 1) * pageSize);
        pd.put("size", pageSize);
        result.setResult(adService.getThemeAds(pd));
        result.setResultNum(adService.getThemeAdsNum(pd));
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("delThemeAd")
    JsonResult delThemeAd(@RequestParam int id) {
        JsonResult result = new JsonResult();
        adService.delThemeAd(id);
        result.setSuccessful();
        return result;
    }

    @LoginRequired
    @PostMapping("editThemeAdState")
    JsonResult editThemeAdState(@RequestParam int id) {
        JsonResult result = new JsonResult();
        adService.editThemeAdState(id);
        result.setSuccessful();
        return result;
    }
}
